package com.undsf.ogd;

import com.undsf.util.Block;
import com.undsf.util.Value;
import com.undsf.util.CsvFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Arathi on 2015/10/19.
 */
public class TalentMarket {
    protected Map<Integer, Peopleware> talents;
    protected Map<Integer, PeoplewareExtend> talentExtendInfos;

    //public TalentMarket() {}

    public TalentMarket(CsvFile meta) {
        boolean success = init(meta);
        if (!success) System.err.println("TalentMarket加载失败！");
        else System.out.println("TalentMarket加载成功！");
    }

    public boolean init(CsvFile meta) {
        int index, startIndex, endIndex;
        Block proplewareExtendInfo = meta.getBlock("Peopleware-Extend");
        startIndex = proplewareExtendInfo.getIndex()+1;
        endIndex = startIndex + proplewareExtendInfo.getRowNum();
        talentExtendInfos = new HashMap<>();
        for (index=startIndex; index<endIndex; index++) {
            try {
                Value cvt = new Value(meta.getLine(index));
                PeoplewareExtend pExtend = new PeoplewareExtend(
                        cvt.getInt(0),
                        cvt.getInt(2),
                        cvt.getInt(3),
                        cvt.getString(6)
                );
                talentExtendInfos.put(pExtend.getId(), pExtend);
            }
            catch (NumberFormatException e){
                System.err.println("载入人员扩展信息出现数据转换异常！" + e.getMessage());
            }
        }
        System.out.println("载入外包人员扩展信息" + talentExtendInfos.size() + "人。");

        Block peoplewaresInfo = meta.getBlock("Peopleware");
        startIndex = peoplewaresInfo.getIndex()+1;
        endIndex = startIndex + peoplewaresInfo.getRowNum();
        talents = new HashMap<>();
        for (index=startIndex; index<endIndex; index++) {
            try {
                //获取数据，生成Peopleware
                Value cvt = new Value(meta.getLine(index));
                //--------11~14---------
                List<Integer> propertys = new ArrayList<>();
                int i;
                for (i = 0; i < 4; i++) {
                    propertys.add(cvt.getInt(11 + i));
                }
                //--------18~25----------
                List<Integer> skillLevels = new ArrayList<>();
                for (i = 0; i < 8; i++) {
                    skillLevels.add(cvt.getInt(18 + i));
                }
                Peopleware people = new Peopleware(
                        cvt.getInt(0),
                        cvt.getInt(1),
                        cvt.getString(2),
                        cvt.getString(3),
                        cvt.getInt(4),
                        cvt.getInt(5),
                        cvt.getInt(6),
                        cvt.getInt(7),
                        cvt.getInt(8),
                        cvt.getInt(9),
                        cvt.getInt(10),
                        propertys, //11-14
                        cvt.getInt(15),
                        cvt.getInt(16),
                        cvt.getInt(17),
                        skillLevels, //18-25
                        cvt.getString(26)
                );
                //合并数据
                if ( people.type>0 ) {
                    int teiID = people.salary;
                    PeoplewareExtend pExtend = talentExtendInfos.get(teiID);
                    people.talent = pExtend.getJob();
                    people.desc = pExtend.getDesc();
                }
                talents.put(people.id, people);
            } catch (NumberFormatException e) {
                System.err.println("载入人员信息出现数据转换异常！" + e.getMessage());
            }
        }
        System.out.println("载入人员信息" + talents.size() + "人。");
        return true;
    }

    public Peopleware getPeopleById(int id){
        return talents.get(id);
    }

    public Map<Integer, Peopleware> getAllPeople(){
        return talents;
    }
}
